//
//  HomeCellCollectionView.swift
//  XLWB
//
//  Created by 郭亮 on 16/3/18.
//  Copyright © 2016年 gl. All rights reserved.
//

import UIKit
import SDWebImage

class HomeCellCollectionView: UICollectionView {

     var pictureUrls: [NSURL] = [NSURL]() {
        didSet {
            // 设置collectionView高度宽度约束
            let collectionViewSize = sizeForCollectionView(pictureUrls.count)
            collectionViewWidthCons.constant = collectionViewSize.width
            collectionViewHeightCons.constant = collectionViewSize.height

            reloadData()
        }
    }
    
    // MARK:-控件属性
    @IBOutlet weak var collectionViewWidthCons: NSLayoutConstraint!
    @IBOutlet weak var collectionViewHeightCons: NSLayoutConstraint!

    
    override func awakeFromNib() {
        super.awakeFromNib()
        //设置数据源
        dataSource = self
        delegate = self
        
        //设置流水布局
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        let imageWH = (UIScreen.mainScreen().bounds.width - 2 * edgeMargin - 2 * paddingMargin) / 3
        layout.itemSize = CGSizeMake(imageWH, imageWH)
        layout.minimumInteritemSpacing = paddingMargin
        layout.minimumLineSpacing = paddingMargin
    }

}

// MARK:-数据源方法
extension HomeCellCollectionView: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureUrls.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCellId", forIndexPath: indexPath) as! HomeCellCollectionViewCell
        cell.picUrl = pictureUrls[indexPath.item]
        return cell
    }
}

// MARK:-代理方法
extension HomeCellCollectionView: UICollectionViewDelegate {
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        //发送通知
        let userInfo = [showPhotoBrowserVcNoteIndex: indexPath, showPhotoBrowserVcNoteUrls: pictureUrls] as [String: AnyObject]
        NSNotificationCenter.defaultCenter().postNotificationName(showPhotoBrowserVcNote, object: nil, userInfo: userInfo)
    }
}

extension HomeCellCollectionView {
    private func sizeForCollectionView(count: Int) -> CGSize {
        //没有url
        if count == 0 {
            return CGSizeMake(0, 0)
        }
        //有4条url
        let cellWH = (UIScreen.mainScreen().bounds.width - 2 * edgeMargin - 2 * paddingMargin) / 3
        if count == 4 {
            let width = cellWH * 2 + paddingMargin
            let height = cellWH * 2 + paddingMargin
            return CGSizeMake(width, height)
        }
        //其他情况
        let row = CGFloat((count - 1) / 3 + 1)
        let width = cellWH * 3 + 2 * paddingMargin
        let height = cellWH * row + paddingMargin * (row - 1)
        return CGSizeMake(width, height)
    }
}

class HomeCellCollectionViewCell: UICollectionViewCell {
    var picUrl: NSURL? {
        didSet {
        imageV.sd_setImageWithURL(picUrl)
        }
    }
    @IBOutlet weak var imageV: UIImageView!
    
    
}
